﻿using System;
using Microsoft.Xna.Framework;
using Phoenix.Xna.Framework.Properties;

namespace Phoenix.Xna.Framework.FrameworkExtensions
{
    public static class StringExtensions
    {
        public static Vector2 ToVector2(this string obj)
        {
            string[] split = obj.Split(',');

            if (split != null && split.Length == 2)
            {
                return new Vector2(float.Parse(split[0]), float.Parse(split[1]));
            }

            return Vector2.Zero;
        }

        public static Vector3 ToVector3(this string obj)
        {
            string[] split = obj.Split(',');

            if (split != null && split.Length == 3)
            {
                return new Vector3(float.Parse(split[0]), float.Parse(split[1]), float.Parse(split[2]));
            }

            return Vector3.Zero;
        }

        public static Vector4 ToVector4(this string obj)
        {
            string[] split = obj.Split(',');

            if (split != null && split.Length == 4)
            {
                return new Vector4(float.Parse(split[0]), float.Parse(split[1]), float.Parse(split[2]), float.Parse(split[3]));
            }

            return Vector4.Zero;
        }

        public static T ParseT<T>(this string obj)
        {
            if (!string.IsNullOrEmpty(obj))
            {
                object data;

                if (typeof(T) == typeof(bool))
                {
                    data = Convert.ToBoolean(obj);
                }
                else if (typeof(T) == typeof(float))
                {
                    data = float.Parse(obj);
                }
                else if (typeof(T) == typeof(int))
                {
                    data = int.Parse(obj);
                }
                else if (typeof(T) == typeof(short))
                {
                    data = short.Parse(obj);
                }
                else if (typeof(T) == typeof(double))
                {
                    data = double.Parse(obj);
                }
                else if (typeof(T) == typeof(Vector2))
                {
                    data = obj.ToVector2();
                }
                else if (typeof(T) == typeof(Vector3))
                {
                    data = obj.ToVector3();
                }
                else if (typeof(T) == typeof(Vector4))
                {
                    data = obj.ToVector4();
                }
                else
                {
                    throw new ArgumentException(string.Format(Resources.ArgumentException, typeof(T).ToString()));
                }

                return (T)data;
            }

            return default(T);
        }
    }
}
